package com.ruicar.afs.cloud.seats.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.seats.entity.BaseHoliday;
import com.ruicar.afs.cloud.seats.mapper.BaseHolidayMapper;
import com.ruicar.afs.cloud.seats.service.BaseHolidayService;
import com.ruicar.afs.cloud.seats.util.DateUtil;
import com.ruicar.afs.cloud.seats.util.WorkFlag;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author tiankai
 * @Describe 工作日历
 * Created on 2020-05-15
 */
@Service
public class BaseHolidayServiceImpl extends ServiceImpl<BaseHolidayMapper, BaseHoliday> implements BaseHolidayService {
    @Override
    public void saveBaseHoliday() throws Exception {
        for (int j = 2020; j <= 2020; j++) {
            String dateStart = DateUtil.formatDate(DateUtil.getYearFirst(j));
            String dateEnd = DateUtil.formatDate(DateUtil.getYearLast(j));
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
            long startTime = date.parse(dateStart).getTime();
            long endTime = date.parse(dateEnd).getTime();
            long day = 1000 * 60 * 60 * 24;
            for (long i = startTime; i <= endTime; i += day) {
                String title = date.format(new Date(i));
                String week = String.valueOf(new Date(i).getDay());
                BaseHoliday baseHoliday = new BaseHoliday();
                baseHoliday.setYear(title.substring(0, 4));
                baseHoliday.setMonth(title.substring(5, 7));
                baseHoliday.setDay(title.substring(8, 10));
                baseHoliday.setWeek(week);
                baseHoliday.setTimeAm(WorkFlag.TIME_AM.getCode());
                baseHoliday.setTimePm(WorkFlag.TIME_PM.getCode());
                baseHoliday.setWorkFlagAm(WorkFlag.WORK_FLAG.getCode());
                baseHoliday.setWorkFlagPm(WorkFlag.WORK_FLAG.getCode());
                if (WorkFlag.SATURDAY.getCode().equals(week) || WorkFlag.SUNDAY.getCode().equals(week)) {
                    baseHoliday.setWorkFlagAm(WorkFlag.REST_FLAG.getCode());
                    baseHoliday.setWorkFlagPm(WorkFlag.REST_FLAG.getCode());
                }
                this.save(baseHoliday);
            }
        }
    }
}
